package com.rfid.sockethelper;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;

import com.rfid.DirectoryBean;
import com.rfid.event.SendStatus;
import com.rfid.util.FileUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;

public class Base {

    /**采用全局静态线程池方式，后续版本再完整替代。*/
//    protected static Executor mPubExecutor = Executors.newCachedThreadPool();
    private InputStream mInputStream = null;
    private OutputStream mOutputStream = null;
    private String ip;
    private Lock mLock;
    protected void init(InputStream inputStream, OutputStream outputStream, String ip, Lock lock) {
        this.mInputStream = inputStream;
        this.mOutputStream = outputStream;
        this.ip = ip;
        this.mLock = lock;
    }

    protected void sendFile(OutputStream outputStream,final Context context, final Uri uri,String serverIP) {
        if (outputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_SEND_FILE,serverIP));
            return;
        }
        try {
            String path = getPath(context, uri);
            boolean r = IOHelper.sendFile(outputStream, path);
            EventBus.getDefault().post(new SendStatus(r?0:2,IOHelper.CMD_SEND_FILE,serverIP));
        } catch (Exception e) {
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_SEND_FILE,serverIP));
        }
    }

    protected void sendFile(final String path) {
        if (mOutputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_SEND_FILE,ip));
            return;
        }
        try {
            File file = new File(path);
            if (!file.exists()) {
                EventBus.getDefault().post(new SendStatus(3,IOHelper.CMD_SEND_FILE,ip));
                return;
            }
            mLock.lock();
            boolean r = IOHelper.sendFile(mOutputStream, path);
            EventBus.getDefault().post(new SendStatus(r?0:2,IOHelper.CMD_SEND_FILE,ip));
        } catch (Exception e) {
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_SEND_FILE,ip));
        } finally {
            mLock.unlock();
        }
    }

    public void sendQueryFileListCmd(String path) {
        System.out.println("sendQueryFileListCmd path = " + path);
        if (mOutputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_REQ_FILE_LIST,ip));
            return;
        }
        System.out.println("base sendQueryFileListCmd");
        try {
            mLock.lock();
            IOHelper.sendQueryFileList(mOutputStream,path);
            EventBus.getDefault().post(new SendStatus(0,IOHelper.CMD_REQ_FILE_LIST,ip));
        } catch (Exception e) {
            e.printStackTrace();
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_REQ_FILE_LIST,ip));
        } finally {
            mLock.unlock();
        }
    }

    public void sendFileList(OutputStream outputStream, DirectoryBean bean,String serverIP) {
        if (outputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_SEND_FILE_LIST,serverIP));
            return;
        }
        try {
            IOHelper.sendFileList(outputStream,bean);
            EventBus.getDefault().post(new SendStatus(0,IOHelper.CMD_SEND_FILE_LIST,serverIP));
        } catch (Exception e) {
            e.printStackTrace();
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_SEND_FILE_LIST,serverIP));
        }
    }

    public void sendReqFileCmd(String path) {
        if (mOutputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_REQ_FILE,ip));
            return;
        }
        try {
            mLock.lock();
            IOHelper.sendReqFileCmd(mOutputStream,path);
            EventBus.getDefault().post(new SendStatus(0,IOHelper.CMD_REQ_FILE,ip));
        } catch (Exception e) {
            e.printStackTrace();
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_REQ_FILE,ip));
        } finally {
            mLock.unlock();
        }
    }

    public void sendFileToPath(OutputStream outputStream,String path,String rpath,String serverIP) {
        if (outputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_SET_FILE,serverIP));
            return;
        }
        try {
            IOHelper.sendSetFile(outputStream,path,rpath);
            EventBus.getDefault().post(new SendStatus(0,IOHelper.CMD_SET_FILE,serverIP));
        } catch (Exception e) {
            e.printStackTrace();
            EventBus.getDefault().post(new SendStatus(2,IOHelper.CMD_SET_FILE,serverIP));
        }
    }

    public void sendFileToPath(OutputStream outputStream,final Context context, final Uri uri,String rpath,String serverIP) {
        if (outputStream == null) {
            EventBus.getDefault().post(new SendStatus(1,IOHelper.CMD_SET_FILE,serverIP));
            return;
        }
        try {
            String path = getPath(context, uri);
            System.out.println("sendFileToPath local path = " + path);
            sendFileToPath(outputStream,path,rpath,serverIP);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getPath(Context context, Uri uri) throws IOException {
        String path = uri.getPath();
        path = path.replace("external_files", "sdcard").replace(":", "").replace("：", "");
        File file = new File(path);
        if (!file.exists()) {
            File tpf = new File("/sdcard/" + Environment.DIRECTORY_DOWNLOADS, file.getName());
            InputStream in = context.getContentResolver().openInputStream(uri);
            FileUtil.streamToFile(in, tpf);
            path = tpf.getPath();
        }
        return path;
    }
}
